from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime
from enum import Enum

class DataCleanTaskStatusEnum(str, Enum):
    """数据清洗任务状态枚举"""
    PENDING = "pending"
    PROCESSING = "processing"
    COMPLETED = "completed"
    FAILED = "failed"

class DataCleanTaskBase(BaseModel):
    """数据清洗任务基础模型"""
    name: str = Field(..., description="任务名称")
    source_type: str = Field(..., description="数据来源类型：file/database")
    file_path: Optional[str] = Field(None, description="文件路径")
    db_config: Optional[Dict[str, Any]] = Field(None, description="数据库配置")

class DataCleanTaskCreate(DataCleanTaskBase):
    """创建数据清洗任务的请求模型"""
    pass

class DataCleanTaskUpdate(BaseModel):
    """更新数据清洗任务的请求模型"""
    status: Optional[str] = Field(None, description="状态")
    error_message: Optional[str] = Field(None, description="错误信息")
    completed_at: Optional[datetime] = Field(None, description="完成时间")
    result_file_path: Optional[str] = Field(None, description="结果文件路径")

class DataCleanTask(DataCleanTaskBase):
    """数据清洗任务返回模型"""
    id: int
    status: str
    error_message: Optional[str] = None
    created_at: datetime
    completed_at: Optional[datetime] = None
    result_file_path: Optional[str] = None
    created_by: str

    class Config:
        from_attributes = True

class DataCleanTaskOut(BaseModel):
    """数据清洗任务输出模型"""
    id: int = Field(..., description="任务ID")
    task_name: str = Field(..., description="任务名称")
    source_type: str = Field(..., description="数据来源类型")
    file_path: str = Field(..., description="文件路径")
    status: str = Field(..., description="任务状态")
    error_message: Optional[str] = Field(None, description="错误信息")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    created_by: str = Field(..., description="创建用户")
    
    class Config:
        from_attributes = True
        json_schema_extra = {
            "example": {
                "id": 1,
                "task_name": "商品数据清洗",
                "source_type": "excel",
                "file_path": "uploads/data_clean/products.xlsx",
                "status": "completed",
                "error_message": None,
                "created_at": "2023-01-01T12:00:00",
                "updated_at": "2023-01-01T12:30:00",
                "created_by": "admin"
            }
        }

# 品牌配置模型
class BrandConfigBase(BaseModel):
    name: str
    category: Optional[str] = None
    is_active: bool = True

class BrandConfigCreate(BrandConfigBase):
    pass

class BrandConfigUpdate(BaseModel):
    name: Optional[str] = None
    category: Optional[str] = None
    is_active: Optional[bool] = None

class BrandConfig(BrandConfigBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

# 商品名称排除模型
class CommonProductNameBase(BaseModel):
    name: str
    category: Optional[str] = None
    description: Optional[str] = None
    is_active: bool = True

class CommonProductNameCreate(CommonProductNameBase):
    pass

class CommonProductNameUpdate(BaseModel):
    name: Optional[str] = None
    category: Optional[str] = None
    description: Optional[str] = None
    is_active: Optional[bool] = None

class CommonProductName(CommonProductNameBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

# 单位配置模型
class UnitConfigBase(BaseModel):
    name: str
    type: Optional[str] = None
    is_active: bool = True

class UnitConfigCreate(UnitConfigBase):
    pass

class UnitConfigUpdate(BaseModel):
    name: Optional[str] = None
    type: Optional[str] = None
    is_active: Optional[bool] = None

class UnitConfig(UnitConfigBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

# 营销词配置模型
class MarketingWordConfigBase(BaseModel):
    word: str
    category: Optional[str] = None
    is_active: bool = True

class MarketingWordConfigCreate(MarketingWordConfigBase):
    pass

class MarketingWordConfigUpdate(BaseModel):
    word: Optional[str] = None
    category: Optional[str] = None
    is_active: Optional[bool] = None

class MarketingWordConfig(MarketingWordConfigBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

# 系统配置模型
class SystemConfigBase(BaseModel):
    key: str
    value: Optional[str] = None
    description: Optional[str] = None

class SystemConfigCreate(SystemConfigBase):
    pass

class SystemConfigUpdate(BaseModel):
    value: Optional[str] = None
    description: Optional[str] = None

class SystemConfig(SystemConfigBase):
    id: int
    updated_at: datetime

    class Config:
        from_attributes = True

# 批量导入模型
class BulkCreateRequest(BaseModel):
    items: List[Dict[str, Any]]

# 批量更新状态模型
class BulkStatusUpdate(BaseModel):
    ids: List[int]
    is_active: bool

# 单位转换规则模型
class UnitConversionBase(BaseModel):
    source_unit: str = Field(..., description="源单位")
    target_unit: str = Field(..., description="目标单位")
    conversion_formula: str = Field(..., description="转换公式，如 'x * 2'")
    description: Optional[str] = Field(None, description="描述")
    is_active: bool = Field(True, description="是否启用")

class UnitConversionCreate(UnitConversionBase):
    pass

class UnitConversionUpdate(BaseModel):
    conversion_formula: Optional[str] = Field(None, description="转换公式")
    description: Optional[str] = Field(None, description="描述")
    is_active: Optional[bool] = Field(None, description="是否启用")

class UnitConversion(UnitConversionBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True
        json_schema_extra = {
            "example": {
                "id": 1,
                "source_unit": "g",
                "target_unit": "斤",
                "conversion_formula": "x / 500",
                "description": "克转斤",
                "is_active": True,
                "created_at": "2023-01-01T12:00:00",
                "updated_at": "2023-01-01T12:00:00"
            }
        }

# 单位转换测试模型
class UnitConversionTest(BaseModel):
    value: float = Field(..., description="要转换的值")
    source_unit: str = Field(..., description="源单位")

class UnitConversionResult(BaseModel):
    original_value: float = Field(..., description="原始值")
    original_unit: str = Field(..., description="原始单位")
    converted_value: float = Field(..., description="转换后的值")
    standard_unit: str = Field(..., description="标准单位")
    success: bool = Field(..., description="是否成功")
    error: Optional[str] = Field(None, description="错误信息")

# 商品名称清洗测试模型
class ProductNameCleanTest(BaseModel):
    product_name: str = Field(..., description="商品名称")

class ProductNameCleanResult(BaseModel):
    original_name: str = Field(..., description="原始商品名称")
    cleaned_name: str = Field(..., description="清洗后的商品名称")
    brand: str = Field(..., description="品牌")
    specification: str = Field(..., description="规格")
    unit: str = Field(..., description="单位")
    price: float = Field(..., description="价格")
    standard_unit: str = Field(..., description="标准单位")
    standard_price: float = Field(..., description="标准价格")
    confidence_score: float = Field(..., description="置信度评分")
    success: bool = Field(..., description="是否成功")
    error: Optional[str] = Field(None, description="错误信息") 